﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KATS_Client.UI
{
    /// <summary>
    /// A search bar
    /// </summary>
    class SearchBar
    {
        /// <summary>Create a search bar for window w</summary>
        /// <param name="w"></param>
        public SearchBar(Window w)
        {
            window = w;
        }

        /// <summary>Get the text in the search box</summary>
        public String Text
        {
            get { return searchBox.Text.Trim(); }
        }

        /// <summary>Create the text in the search box</summary>
        public void Clear()
        {
            if (searchBox.Text != string.Empty)
            {
                searchBox.Text = string.Empty;
                UpdateView();
            }
        }

        /// <summary>Is search bar visible?</summary>
        bool _visible = false;

        /// <summary>The visible status has changed</summary>
        public event EventHandler<EventArgs> Visible_Changed;

        /// <summary>Is the search bar visible?</summary>
        public bool Visible
        {
            get { return _visible; }
            private set
            {
                bool old = _visible;
                _visible = value;

                if (old != _visible)
                {
                    // then value has changed
                    if (Visible_Changed != null)
                        Visible_Changed(this, null);
                }

            }
        }

        /// <summary>The height of the search bar</summary>
        int Height = 21;

        /// <summary>The width/height of the close button</summary>
        int HideButton_Size = 19;

        /// <summary>The window this search bar belongs to</summary>
        Window window = null;


        /// <summary>The search panel for this window</summary>
        FlowLayoutPanel _SearchPanel = null;
        /// <summary>The search panel for this window</summary>
        public FlowLayoutPanel SearchPanel
        {
            get
            {
                if (_SearchPanel == null)
                    Create();
                return _SearchPanel;
            }
            private set { _SearchPanel = value; }
        }

        /// <summary>The box where the user types in the filter term</summary>
        TextBox searchBox = null;

        /// <summary>The button that hides the search box</summary>
        Button HideButton = null;

        /// <summary>Create a search bar</summary>
        void Create()
        {
            SearchPanel = new FlowLayoutPanel();
            SearchPanel.Height = Height;
            int pSize = 2;
            SearchPanel.Padding = new Padding(pSize, 0, pSize, 0);
            SearchPanel.Dock = DockStyle.Bottom;

            Label text = new Label();
            text.Text = "Filter:";
            text.Width = 33;
            text.TextAlign = System.Drawing.ContentAlignment.TopRight;
            text.Margin = new Padding(0);
            text.Padding = new Padding(0);

            FlowLayoutPanel searchBoxPanel = new FlowLayoutPanel();
            searchBoxPanel.BackColor = System.Drawing.Color.Gray;
            searchBoxPanel.Height = 15;
            searchBoxPanel.Padding = new Padding(1, 1, 1, 1);
            searchBoxPanel.Margin = new Padding(0);
            searchBoxPanel.FlowDirection = FlowDirection.LeftToRight;
            searchBoxPanel.Width += 2;
            searchBox = new TextBox();
            searchBox.Width *= 2;
            searchBox.Margin = new Padding(0, 0, 1, 0);
            searchBox.BorderStyle = BorderStyle.None;
            searchBox.Height = Height - 2;
            searchBoxPanel.Controls.Add(searchBox);
            searchBox.KeyUp += new KeyEventHandler(searchBox_KeyUp);


            HideButton = new Button();
            HideButton.Image = Properties.Resources.Close;
            HideButton.MouseEnter += new EventHandler(close_MouseEnter);
            HideButton.MouseLeave += new EventHandler(close_MouseLeave);
            HideButton.MouseClick += new MouseEventHandler(close_MouseClick);
            HideButton.Padding = new Padding(0);
            HideButton.Margin = new Padding(0);
            HideButton.Top = 3;
            HideButton.Width = HideButton_Size;
            HideButton.Height = HideButton_Size;
            HideButton.FlatStyle = FlatStyle.Flat;
            HideButton.FlatAppearance.BorderSize = 0;
            HideButton.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
            HideButton.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
            HideButton.TextImageRelation = TextImageRelation.Overlay;
            HideButton.Name = "Close";
            HideButton.ImageAlign = System.Drawing.ContentAlignment.BottomCenter;

            // add a tool tip to the hide button
            ToolTip tip = new ToolTip();
            tip.AutoPopDelay = 6000;
            tip.InitialDelay = 1000;
            tip.ReshowDelay = 500;
            tip.ShowAlways = true;
            tip.SetToolTip(HideButton, HideButton.Text);


            SearchPanel.Controls.Add(HideButton);
            SearchPanel.Controls.Add(text);
            SearchPanel.Controls.Add(searchBoxPanel);
        }

        /// <summary>A key has been pressed in the search box area
        ///             Call refresh in the viewed tab</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void searchBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                // clear the expression and hide the menu item
                searchBox.Text = string.Empty;
                Hide();
                return;
            }
            if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Enter || e.KeyCode == Keys.Delete)
            { 
                // backspace key or enter key or delete key pressed
                // so worth an update
                UpdateView();
                return;
            }
            if ((int)e.KeyCode < (int)Keys.D0 || (int)e.KeyCode > (int) Keys.Divide)
                return;
            UpdateView();
        }

        /// <summary>
        /// Update the tab view
        /// </summary>
        void UpdateView()
        {
            TabControl tc = this.window.tabControl;
            TabPage page = tc.SelectedTab;
            Tab tab = (Tab)page.Tag;
            tab.DisplayData();
        }



        /// <summary>When close button is pressed, hide the search panel</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void close_MouseClick(object sender, MouseEventArgs e)
        {
            Hide();
        }

        /// <summary>Restore the icon of the button to the normal icon</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void close_MouseLeave(object sender, EventArgs e)
        {
            ((Button)sender).Image = Properties.Resources.Close;
        }

        /// <summary>Change the icon of the button to the hover icon</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void close_MouseEnter(object sender, EventArgs e)
        {
            ((Button)sender).Image = Properties.Resources.CloseOver;
        }

        /// <summary>Show the search box</summary>
        public void Show()
        {
            Visible = true;
            TableLayoutPanel tlp = (TableLayoutPanel)window.tabControl.Parent;
            tlp.RowStyles[2].Height = Height;
            searchBox.Focus();
            searchBox.SelectAll();
        }

        /// <summary>Hide the search box</summary>
        public void Hide()
        {
            Visible = false;
            TableLayoutPanel tlp = (TableLayoutPanel)window.tabControl.Parent;
            tlp.RowStyles[2].Height = 0;
            UpdateView();
        }
    }
}
